package com.framework.dubbo.producer.file2html;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.framework.dubbo.producer.config.Constants;
import com.framework.dubbo.producer.utils.AlgorithmUtil;
import com.framework.dubbo.producer.utils.PdfBoxUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * 路径文件预览
 * @author xianggj
 * @Date 2021/6/8 11:07
 **/
public class PathPreviewUtil extends AbstractFilePreviewTools{
    @Override
    public String convert2Html(String path) {
        return path2Html(path);
    }

    @Override
    public String convert2Html(InputStream in, String ext) {
        String filePath = writeToLocal(in, ext) ;
        return path2Html(filePath);
    }

    @Override
    public String convert2Str(InputStream in, String ext) {
        String result = PdfBoxUtils.pdf2Str(in);
        if(StrUtil.isEmpty(result)) {
            AlgorithmUtil algorithmUtil = SpringUtil.getBean(AlgorithmUtil.class);
            result = algorithmUtil.ocr(in, System.currentTimeMillis() + Constants.DOT + ext);
        }
        return result;
    }


    public static String path2Html(String path) {
        StringBuffer sb = new StringBuffer();
        sb.append("<html>");
        sb.append("<head>");
        sb.append("<meta name='viewport' content='width=device-width, minimum-scale=0.1'>");
        sb.append("</head>");
        sb.append("<body style='margin: 0px;'>");
        String src = "<iframe src='%s' ";
        sb.append(String.format(src, path));
        sb.append(" frameborder='0' style='width:100%; height: 100%'></iframe>");
        sb.append("</body>");
        sb.append("</html>");
        return sb.toString();
    }

    public static void main(String[] args) throws FileNotFoundException {
        File sourceFile = new File("C:\\Users\\ZHAOJIAN\\Desktop\\测试文件\\0524修改意见.pdf");
        FileInputStream fis = new FileInputStream(sourceFile);
        System.out.println(new PathPreviewUtil().convert2Str(fis, "csv"));
    }
}
